#include <corecrt_malloc.h>
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int QDateType;
typedef struct QueueNode {
	int date;
	struct QueueNode* next;
}QueueNode;

typedef struct Queue {
	QueueNode* head;
	QueueNode* tail;
	int size;
}Queue;
typedef int QDateType;


void QueueInit(Queue* q);
void QueueDestroy(Queue* q);
void QueuePush(Queue* q, QDateType x);
void QueuePop(Queue* q);
bool QueueEmpty(Queue* q);
int QueueSize(Queue* q);
QDateType QueueFront(Queue* q);
QDateType QueueBack(Queue* q);

void QueueInit(Queue* q)
{
	q->head = q->tail = NULL;
	q->size = 0;
}
void QueueDestroy(Queue* q)
{
	QueueNode* cur = q->head;
	while (cur)
	{
		QueueNode* tmp = cur->next;
		free(cur);
		cur = tmp;
	}
	q->head = q->tail = NULL;
	q->size = 0;
}
void QueuePush(Queue* q, QDateType x)
{
	QueueNode* tmp = (QueueNode*)malloc(sizeof(QueueNode));
	if (tmp == NULL)
	{
		perror("Malloc Failed");
		return;
	}
	tmp->date = x;
	tmp->next = NULL;
	if (!q->head)
	{
		q->head= tmp;
		q->tail = tmp;
	}
	else
	{
		q->tail->next = tmp;
		 q->tail= tmp;
	}
	q->size++;
}
void QueuePop(Queue* q)
{
	assert(!QueueEmpty(q));
	assert(q->head != NULL);
	if (!q->head->next)
	{
		free(q->head);
		q->head = q->tail = NULL;
	}
	else
	{
		QueueNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
	q->size--;

}
bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->size == 0;
}
int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}
QDateType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->head->date;
}
QDateType QueueBack(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->tail->date;
}

typedef struct {
    Queue q1;
    Queue q2;
} MyStack;

MyStack* myStackCreate() {
    MyStack *mystack=(MyStack*)malloc(sizeof(MyStack));
    QueueInit(&mystack->q1);
    QueueInit(&mystack->q2);
    return mystack;
}

void myStackPush(MyStack* obj, int x) {
    
	if(!QueueEmpty(&obj->q1))
	{
		QueuePush(&obj->q1,x);
	}
	else  
	{
		QueuePush(&obj->q2,x);
	}


}

int myStackPop(MyStack* obj) {
    Queue* noempty=&obj->q1;
	Queue* empty=&obj->q2;
	if(QueueEmpty(&obj->q1))
	{
		empty=&obj->q1;
		noempty=&obj->q2;
	}
	while(noempty->size>1)
	{
		QueuePush(empty,QueueFront(noempty));
		QueuePop(noempty);
	}
	QDateType v=noempty->head->date;
	QueuePop(noempty);
	return v;
}

int myStackTop(MyStack* obj) {
	Queue* noempty=&obj->q1;
	Queue* empty=&obj->q2;
	if(QueueEmpty(&obj->q1))
	{
		empty=&obj->q1;
		noempty=&obj->q2;
	}
	return QueueBack(noempty);
}

bool myStackEmpty(MyStack* obj) {
	Queue* noempty=&obj->q1;
	Queue* empty=&obj->q2;
	return QueueEmpty(noempty)&&QueueEmpty(empty);
}

void myStackFree(MyStack* obj) {
	QueueDestroy(&obj->q1);
	QueueDestroy(&obj->q2);
	free(obj);
	obj=NULL;
}